// pages/music/musicItem/musicItem.js

var util = require("../../../utils/util.js");

//init player
const player = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */

  data: {
    rlvCourseList:[],
    detail: {},
    CommList:[],
    detailHidden:true,
    itemHidden:false,
    commentHidden:true,
    playerIcon: "C",
    currentTime: "00:00",
    endTime: "00:00",
    value:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //get detail
    var MsgID = options.MsgID;
    var Token = "";
    var page = this;
    var basePath = getApp().globalData.basePath.miniAppPath;
    var sessionId = getApp().globalData.sessionId;
    var userId = getApp().globalData.userId;
    if (userId==""){
      wx.showToast({
        title: '授权失败',
      });
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        });
      }, 1000);
    }
    wx.request({
      url: basePath+"/wxMa/3/" +userId ,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "JSESSIONID=" + sessionId 
      },
      success: function (res) {
        
        if (res.data.code == "21") {
          Token = res.data.token;
          var params = {
            Param: {
              MsgID: MsgID,
              Token: Token
            },
            Action: 6212
          };
          util.reqMedia(params, function (value) {
           
            var detail = value;
            var typeName = value.TypeName;
            detail.TypeName = util.sToN(typeName);
              
            // get media url
            var VideoID = detail.MediaID;
            params = {
              Param: {
                VideoID: VideoID
              },
              Action: 4120
            };
            util.reqMedia(params, function (value) {

              player.src = value.VideoPath;
            });

       
            page.setData({
              detail: detail
            });
          });
        }
      }
    });

    // get comment
    var params = {
      Param: {
        MsgID: MsgID,
        PageIndex: 1,
        ShowIndexNum: 50
      },
      Action: 6230
    };
    util.reqMedia(params, function (value) {
      page.setData({
        CommList: value.CommList
      });
    });
 
    

    //get rlvCourseList
    params = {
      Param: {
        BusinessID: MsgID,
        PageIndex: 1,
        ShowIndexNum: 50
      },
      Action: 18025
    };
    util.reqMedia(params, function (value) {
      page.setData({
        rlvCourseList: value.PackageList
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * events on player
   */
  onPlay: function () {
    const playerIcon = this.data.playerIcon;
    if (playerIcon == "C") {
      //begin to play
      this.setData({
        playerIcon: "D"
      });
      player.play();
      var that = this;
      var intervalId = setInterval(function(){
        util.updateSlider(that,player);       
      }, 500);
      this.setData({
        intervalId: intervalId
      });
    } else {
      //stop play
      this.setData({
        playerIcon: "C"
      });
      player.pause();
    }

  },
  onDrag:function(e){

        var value = e.detail.value;
        var duration = player.duration;
        var currentTime = Math.floor(value / 100 * duration);
        player.seek(currentTime);
         currentTime = util.longToTime(currentTime*1000);
        this.setData({
          currentTime: currentTime
        });
        

  },
  
  /**
   * show the Detail
   */
  onShowDetail: function () {
    this.setData({
      detailHidden: false,
      itemHidden: true
    });
  },
  onShowComment:function(){
        this.setData({
          commentHidden:false,
          itemHidden:true
        });
  },
  onBack: function () {
    this.setData({
      detailHidden: true,
      commentHidden:true,
      itemHidden: false
    });
  }
})